import {Component, OnInit} from '@angular/core';
import {BasicService} from "../../unit/basic-service";
import {ApplicationItem} from "../../modals/basic.modal";

@Component({
    selector: 'app-application',
    templateUrl: './application.page.html',
    styleUrls: ['../main/main.page.scss', './application.page.scss'],
})
export class ApplicationPage implements OnInit {
    followList: Array<ApplicationItem> = [];
    allList: Array<ApplicationItem> = [];
    idCard: string;

    constructor(private basicService: BasicService) {
    }

    ngOnInit() {
        this.getApplication();
    }

    getApplication() {
        this.followList = [];
        this.allList = [];
        this.idCard = localStorage.getItem('cardNumber');
        this.basicService.getData('index/getFollow', {idCard: this.idCard}).subscribe(res => {
            this.reGroupApplication('followList', res, 1);
        });
        this.basicService.getData('index/getFollow', {idCard: this.idCard}).subscribe(res => {
            this.reGroupApplication('allList', res, 0);
        });
    }

    reGroupApplication(type, data, status) {
        const colorList = ['#27E2B6', '#00B6ED', '#00D0DE', '#D2A5FF'];
        const newData = [];
        data.map((item, index) => {
            const num = index % 1 ? 1 : index % 2 ? 2 : index % 3 ? 3 : 0;
            item.bg = colorList[num];
            item.app_icon = `./assets/image/main/a-icon-0${num + 1}.png`;
            if(item.sfgz === status){
                newData.push(item);
            }
        });
        this[type] = [...newData];
    }

    cancelFollow(item) {
        this.basicService.postData('index/cancel',
            {followid: item.followid}).then(res => {
            this.getApplication();
        });
    }

    addFollow(item) {
        this.basicService.postData('index/follow',
            {yyid: item.id, idCard: this.idCard}).then(res => {
            this.getApplication();
        });
    }
}
